//
//  ConsultationViewController.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/1/30.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

enum HomeVCSelect {
    case news
    case video
    case use
}

class ConsultationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, BottomShadow, FalseData {
    let banners = ["banner0.jpg", "banner1.jpg", "banner2.jpg", "banner3.jpg", "banner4.jpg"]
    var models: [ConsultaionHomeListModel] = []
    var videoModels: [VideoModel] = []
    static let bannerHeight: CGFloat = 211
    static let pageControlHeight: CGFloat = 25
    var bannerView: FSPagerView = FSPagerView()
    var pageControl: FSPageControl = FSPageControl()
    @IBOutlet weak var mainTableView: BaseTableView!
    var vcType: HomeVCSelect
    var consultaionHomeListModel: [ConsultaionHomeListModel] = []
    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?, vcType: HomeVCSelect) {
        self.vcType = vcType
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        models = falseData()
        videoModels = videoFalseData()
        // Do any additional setup after loading the view.
    }
    
    func setUI() {
        
        mainTableView.tableHeaderView = setBannerData()
        setTableView()
        
        mainTableView.refreshHeader {[unowned self] in
            self.request(isLoadMore: false)
        }
        
        mainTableView.refreshFooter {[unowned self] in
            self.request(isLoadMore: true)
        }
    }
    
    func request(isLoadMore: Bool) {
        HomeViewModel(requestUrl: "", parameters: nil).requestData(isLoadMore: isLoadMore, complete: { (model, isMore) in
            self.endRefresh(isMore: isMore)
        }) { (error, isMore) in
            self.endRefresh(isMore: isMore)
        }
    }
    
    func endRefresh(isMore: Bool) {
        mainTableView.mj_header.endRefreshing()
        mainTableView.mj_footer.endRefreshing()
        mainTableView.mj_footer.isHidden = !isMore
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
    }
    
    func setTableView() {
        switch vcType {
        case .news:
            mainTableView.register(UINib(nibName: "\(ConsultaionTableViewCell.self)", bundle: nil), forCellReuseIdentifier: "ConsultaionCell")
        case .video:
            mainTableView.register(UINib(nibName: "\(HomeVideoTableViewCell.self)", bundle: nil), forCellReuseIdentifier: "HomeVideocell")
        case .use:
            mainTableView.register(UINib(nibName: "\(ConsultaionTableViewCell.self)", bundle: nil), forCellReuseIdentifier: "ConsultaionCell")
        }
        
        mainTableView.estimatedRowHeight = 100;
        mainTableView.rowHeight = UITableViewAutomaticDimension;
        
    }
    
    //MARK: - UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch vcType {
        case .news:
            return models.count
        case .video:
            return videoModels.count
        case .use:
            return videoModels.count
        }
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: UITableViewCell?
        switch vcType {
        case .news:
            cell = tableView.dequeueReusableCell(withIdentifier: "ConsultaionCell")
            let tempCell = cell as? ConsultaionTableViewCell
//            tempCell?.showConsultaionData(model: models[indexPath.row])
        case .video:
            cell = tableView.dequeueReusableCell(withIdentifier: "HomeVideocell") as? HomeVideoTableViewCell
            let tempCell = cell as? HomeVideoTableViewCell
            tempCell?.showData(model: videoModels[indexPath.row])
        case .use:
            cell = tableView.dequeueReusableCell(withIdentifier: "ConsultaionCell") as? ConsultaionTableViewCell
            let tempCell = cell as? ConsultaionTableViewCell
            tempCell?.showVideoData(model: videoModels[indexPath.row])
        }
        
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let consultationDetailVC = ConsultationDetailViewController(nibName: "\(ConsultationDetailViewController.self)", bundle: nil, type: vcType)
        var title: String = ""
        switch vcType {
        case .news:
            title = "资讯详情"
//            consultationDetailVC.model = self.models[indexPath.row]
        case .video:
            title = "视频详情"
             consultationDetailVC.videoModel = self.videoModels[indexPath.row]
        case .use:
            title = "会用"
             consultationDetailVC.videoModel = self.videoModels[indexPath.row]
        }
       
        consultationDetailVC.title = title
        consultationDetailVC.showRightButtonType = .all
        navigationController?.pushViewController(consultationDetailVC, animated: true)
    }
}

extension ConsultationViewController: FSPagerViewDelegate, FSPagerViewDataSource {
    func setBannerData() -> UIView {
        bannerView.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: kHeight(r: ConsultationViewController.bannerHeight))
        bannerView.delegate = self
        bannerView.dataSource = self
        bannerView.isInfinite = true
        bannerView.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "cell")
        pageControl.frame = CGRect(x: 0, y: bannerView.height - ConsultationViewController.pageControlHeight, width: bannerView.width, height: ConsultationViewController.pageControlHeight)
        pageControl.numberOfPages = banners.count
        pageControl.contentHorizontalAlignment = .center
        pageControl.contentInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        pageControl.itemSpacing = 8
        
        let mainView = UIView()
        var rect = bannerView.frame
        rect.size.height += 10
        mainView.frame = rect
        mainView.backgroundColor = ONColor.cellBGColor
        mainView.addSubview(bannerView)
        mainView.addSubview(pageControl)
        setShadow(view: bannerView, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: 0)
        
        return mainView
    }
    
    // MARK:- FSPagerView DataSource
    
    public func numberOfItems(in pagerView: FSPagerView) -> Int {
        return banners.count
    }
    
    public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
        cell.isDescribeLabel = vcType != .use
        cell.imageView?.image = UIImage(named: banners[index])
        cell.imageView?.contentMode = .scaleAspectFill
        cell.imageView?.clipsToBounds = true
        cell.textLabel?.text = "这才是真正的新闻"
        cell.textLabel?.font = UIFont.systemFont(ofSize: 15)
        return cell
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        pagerView.deselectItem(at: index, animated: true)
        pagerView.scrollToItem(at: index, animated: true)
        self.pageControl.currentPage = index
        let model1 = self.banners[index]
        var model: BannerModel = BannerModel()
        model.coverPath = model1
        bannerJump(model: model)
    }
    
    func pagerViewDidScroll(_ pagerView: FSPagerView) {
        guard self.pageControl.currentPage != pagerView.currentIndex else {
            return
        }
        self.pageControl.currentPage = pagerView.currentIndex
    }
    
    
}
